import 'dart:async';
import 'package:duuchin/root_page.dart';
import 'package:flutter/material.dart';

class TransitPage extends StatefulWidget {
  const TransitPage({Key key}) : super(key: key);

  @override
  _TransitPageState createState() => _TransitPageState();
}

class _TransitPageState extends State<TransitPage> {
  Timer _time;
  int _current = 5;

  @override
  void initState() {
    super.initState();
    //  定义定时器
    _time = Timer.periodic(Duration(milliseconds: 1000), (timer) {
      setState(() {
        _current --;
      });
      if(_current <= 0){
        _jumpRoot();
      }
    });
  }

  // 跳转首页的方法
  void _jumpRoot(){
    _time.cancel();
    Navigator.pushAndRemoveUntil(context,MaterialPageRoute(builder: (BuildContext context){
      return RootPage();
    }) , (route) => false);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Stack(
        children: <Widget>[
          Image.asset(
            'assets/images/common/page.png',
            fit: BoxFit.cover,
            width: MediaQuery.of(context).size.width,
            height: MediaQuery.of(context).size.height,
          ),
          Positioned(
            top: MediaQuery.of(context).padding.top + 10,
            right: 10,
            child: InkWell(
              child: _clipButton(),
              onTap: _jumpRoot,
            ),
          )
        ],
      ),
    );
  }

  Widget _clipButton() {
    return ClipRRect(
      borderRadius: BorderRadius.circular(50),
      child: Container(
        width: 50,
        height: 50,
        color: Colors.black.withOpacity(0.5),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              '跳过',
              style: TextStyle(
                color: Colors.white,
                fontSize: 12,
              ),
            ),
            Text(
              '${_current}s',
              style: TextStyle(
                color: Colors.white,
                fontSize: 12,
              ),
            )
          ],
        ),
      ),
    );
  }
}
